package de.szut.fragments;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import de.szut.entity.User;
import de.szut.logic.Logic;
import de.szut.splasher.R;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nbothmer
 * Date: 15.10.13
 * Time: 09:43
 * To change this template use File | Settings | File Templates.
 */
public class WorldTabFragment extends Fragment {
    private TextView deathUserView;
    private TextView nonInfectedUserView;
    private TextView infectedUserView;
    private double values[] = new double[3];
    private long[] percentInNum = new long[3];
    private int[] colors = {Color.BLUE, Color.RED, Color.BLACK};
    private String[] chartCode = {"Gesunde Benutzer", "Infizierte Benutzer", "Vernichtete Benutzer"};
    private Logic logic;
    private ViewGroup chartView;
    private GraphicalView chartViewGraph;
    private List<User> users;

    public WorldTabFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.world_tab_view, container, false);
        logic = Logic.getInstance(getActivity());
        this.chartView = (ViewGroup) root.findViewById(R.id.chart_view_infected);
        this.infectedUserView = (TextView) root.findViewById(R.id.infected_view);
        this.nonInfectedUserView = (TextView) root.findViewById(R.id.nonInfected_view);
        this.deathUserView = (TextView) root.findViewById(R.id.death_view);
        this.setValues();
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        setValues();
    }

    public void setValues() {
        this.values = calculateValue(logic.getAllUsersInCountriesMap());
        if (chartViewGraph == null) {
            this.chartViewGraph = generateChart(this.values);
            this.chartView.addView(chartViewGraph, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        } else {
            chartViewGraph.repaint();
        }
//        this.chartView.removeAllViews();
        this.nonInfectedUserView.setText(chartCode[0] + " : " + Long.toString(percentInNum[0]));
        this.nonInfectedUserView.setTextColor(colors[0]);
        this.infectedUserView.setText(chartCode[1] + " : " + Long.toString(percentInNum[1]));
        this.infectedUserView.setTextColor(colors[1]);
        this.deathUserView.setText(chartCode[2] + " : " + Long.toString(percentInNum[2]));
        this.deathUserView.setTextColor(colors[2]);
    }

    private GraphicalView generateChart(double[]values) {
        GraphicalView view;
        // Instantiating CategorySeries to plot Pie Chart
        CategorySeries distributionSeries = new CategorySeries("Infektions-Übersicht");
        for (int i = 0; i < values.length; i++) {
            // Adding a slice with its values and name to the Pie Chart
            distributionSeries.add(chartCode[i] + " : " + percentInNum[i], values[i]);
        }


        // Instantiating a renderer for the Pie Chart
        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int i = 0; i < values.length; i++) {
            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
            seriesRenderer.setColor(colors[i]);
            seriesRenderer.setDisplayChartValues(true);
            // Adding a renderer for a slice
            defaultRenderer.addSeriesRenderer(seriesRenderer);
        }
        defaultRenderer.setInScroll(true);
        defaultRenderer.setShowLabels(false);
        defaultRenderer.setShowLegend(false);
        defaultRenderer.setPanEnabled(false);
        defaultRenderer.setScale(1.1f);
        defaultRenderer.setZoomEnabled(false);
        defaultRenderer.setClickEnabled(true);
        view = ChartFactory.getPieChartView(getActivity(), distributionSeries, defaultRenderer);
        return view;
    }

    private double[] calculateValue(HashMap<String, ArrayList<User>> allUserInCountries) {
        long infectedUser = 0;
        long nonInfectedUser = 0;
        long deathUser = 0;
        long totalUser = 0;
        double[] values = new double[3];
        users = new ArrayList<User>();
        for (String s : allUserInCountries.keySet()) {
            users.addAll(allUserInCountries.get(s));
        }
        for (User user : users) {
            if (user != null) {
                totalUser += user.getTotalUsers();
                infectedUser += user.getInfectedUsers();
                nonInfectedUser += user.getNotInfectedUsers();
                deathUser += user.getControlledUsers();
            }
        }
        percentInNum[0] = nonInfectedUser;
        percentInNum[1] = infectedUser;
        percentInNum[2] = deathUser;
        if (infectedUser != 0) {

            infectedUser = (infectedUser / totalUser) * 100;
            Log.d("1337_Infected", "" + infectedUser);
        }
        if (deathUser != 0) {
            deathUser = (deathUser / totalUser) * 100;
            Log.d("1337_death", "" + deathUser);
        }
        if (nonInfectedUser != 0) {
            nonInfectedUser = (nonInfectedUser / totalUser) * 100;
            Log.d("1337_nonInfected", "" + nonInfectedUser);
        }
        values[0] = nonInfectedUser;
        values[1] = infectedUser;
        values[2] = deathUser;


        return values;
    }
}
